package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置管理
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingControlller {

    @Reference
    private OrderSettingService orderSettingService;

    //Excel文件上传，解析并保存到数据库
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){

        try {
            //使用POI工具类进行文件解析 [2020/3/19,200]...
            List<String[]> list = POIUtils.readExcel(excelFile);

            //把list集合中每一天的数据取出来，封装到OrderSetting实体类中,并最终把实体类对象封装到新创建的orderSettingList集合中
            if(list != null && list.size() > 0){
                //创建集合用于接收数据
                List<OrderSetting> orderSettingList = new ArrayList<>();

                //遍历list 集合把其中数据封装到OrderSetting实体类中(list中的数据是预约日期和可预约人数)
                for (String[] strings : list) {

                    //list装的都是string类型的数据，先进行类型转换
                    Date date = new Date(strings[0]);
                    int num = Integer.parseInt(strings[1]);
                    //封装到实体类中
                    OrderSetting orderSetting = new OrderSetting(date,num);

                    //再把实体类对象存储到新集合中
                    orderSettingList.add(orderSetting);
                }

                //调用service方法进行批量存储
                orderSettingService.add(orderSettingList);

            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    //根据当前日期查询数据库  2020-03
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            //{ date: 11, number: 120, reservations: 1 }  前端需要的是键值对形式的数据，所以list集合封装的是map集合
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }

    //根据日期修改预约设置
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.editNumberByDate(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }

        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}
